package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.nlf.calendar.Lunar;

import java.text.SimpleDateFormat;
import java.util.Date;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.WeddingBean;
import flc.ast.databinding.ActivityCardPreviewBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class CardPreviewActivity extends BaseAc<ActivityCardPreviewBinding> {

    public static WeddingBean sWeddingBean;
    public static int position;

    @Override
    protected int onCreate() {
        return R.layout.activity_card_preview;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivCardPreviewBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivCardPreviewDownload.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivCardPreviewDownload://下载
                saveImg();
                break;
        }
    }

    @Override
    protected void initData() {
        setData();
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlSave);
                        //保存到相册
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        //保存到历史记录
                        String filePath = FileUtil.generateFilePath(Content.myCardRecord, ".png");
                        ImageUtils.save(bitmap, filePath, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_to_album);
                        finish();
                    }
                });
            }
        }, 1000);
    }

    private void setData() {
        clearView();
        long time = TimeUtils.string2Millis(sWeddingBean.getWeddingDate(), new SimpleDateFormat("yyyy-MM-dd HH:mm"));
        Lunar lunar = Lunar.fromDate(new Date(time));
        switch (position) {
            case 0:
                mDataBinding.rlTemplate1.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(sWeddingBean.getCoverPath()).into(mDataBinding.ivTemplate1Img);
                mDataBinding.tvTemplate1Name.setText(getString(R.string.template_text2) + sWeddingBean.getGroomName() +
                        " & " + getString(R.string.template_text3) + sWeddingBean.getBrideName());
                String invite = "<u>" + sWeddingBean.getGroomName() + "，" + sWeddingBean.getBrideName() +
                        "   " + getString(R.string.template_text5) + "</u>";
                mDataBinding.tvTemplate1Invite.setText(Html.fromHtml(invite));
                mDataBinding.tvTemplate1GongLi.setText(getString(R.string.template_text7) + TimeUtils.millis2String(time, new SimpleDateFormat("yyyy年MM月dd日")));
                mDataBinding.tvTemplate1NongLi.setText(getString(R.string.template_text8, lunar.getMonthInChinese(), lunar.getDayInChinese()) +
                        " " + TimeUtils.millis2String(time, new SimpleDateFormat("EEE")));
                mDataBinding.tvTemplate1Address.setText(sWeddingBean.getWeddingAddress());
                break;
            case 1:
                mDataBinding.rlTemplate2.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(sWeddingBean.getCoverPath()).into(mDataBinding.ivTemplate2Img);
                mDataBinding.tvTemplate2Groom.setText(sWeddingBean.getGroomName());
                mDataBinding.tvTemplate2Bride.setText(sWeddingBean.getBrideName());
                mDataBinding.tvTemplate2GongLi.setText(getString(R.string.template_text7) + TimeUtils.millis2String(time, new SimpleDateFormat("yyyy年MM月dd日 HH:mm")));
                mDataBinding.tvTemplate2NongLi.setText(getString(R.string.template_text8, lunar.getMonthInChinese(), lunar.getDayInChinese()) +
                        " " + TimeUtils.millis2String(time, new SimpleDateFormat("EEE")));
                mDataBinding.tvTemplate2Address.setText(sWeddingBean.getWeddingAddress());
                break;
            case 2:
                mDataBinding.rlTemplate3.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(sWeddingBean.getCoverPath()).into(mDataBinding.ivTemplate3Img);
                mDataBinding.tvTemplate3Groom.setText(sWeddingBean.getGroomName());
                mDataBinding.tvTemplate3Bride.setText(sWeddingBean.getBrideName());
                mDataBinding.tvTemplate3GongLi.setText(TimeUtils.millis2String(time, new SimpleDateFormat("yyyy年MM月dd日 HH:mm")));
                mDataBinding.tvTemplate3Address.setText(sWeddingBean.getWeddingAddress());
                break;
            case 3:
                mDataBinding.rlTemplate4.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(sWeddingBean.getCoverPath()).into(mDataBinding.ivTemplate4Img);
                mDataBinding.tvTemplate4Groom.setText(sWeddingBean.getGroomName());
                mDataBinding.tvTemplate4Bride.setText(sWeddingBean.getBrideName());
                mDataBinding.tvTemplate4GongLi.setText(getString(R.string.template_text7) + TimeUtils.millis2String(time, new SimpleDateFormat("yyyy年MM月dd日 HH:mm")));
                mDataBinding.tvTemplate4NongLi.setText(getString(R.string.template_text8, lunar.getMonthInChinese(), lunar.getDayInChinese()) +
                        " " + TimeUtils.millis2String(time, new SimpleDateFormat("EEE")));
                mDataBinding.tvTemplate4Address.setText(sWeddingBean.getWeddingAddress());
                break;
            case 4:
                mDataBinding.rlTemplate5.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(sWeddingBean.getCoverPath()).into(mDataBinding.ivTemplate5Img);
                mDataBinding.tvTemplate5Groom.setText(sWeddingBean.getGroomName());
                mDataBinding.tvTemplate5Bride.setText(sWeddingBean.getBrideName());
                mDataBinding.tvTemplate5GongLi.setText(getString(R.string.template_text7) + TimeUtils.millis2String(time, new SimpleDateFormat("yyyy年MM月dd日 HH:mm")));
                mDataBinding.tvTemplate5NongLi.setText(getString(R.string.template_text8, lunar.getMonthInChinese(), lunar.getDayInChinese()) +
                        " " + TimeUtils.millis2String(time, new SimpleDateFormat("EEE")));
                mDataBinding.tvTemplate5Address.setText(sWeddingBean.getWeddingAddress());
                break;
            case 5:
                mDataBinding.rlTemplate6.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(sWeddingBean.getCoverPath()).into(mDataBinding.ivTemplate6Img);
                mDataBinding.tvTemplate6Groom.setText(sWeddingBean.getGroomName());
                mDataBinding.tvTemplate6Bride.setText(sWeddingBean.getBrideName());
                mDataBinding.tvTemplate6GongLi.setText(TimeUtils.millis2String(time, new SimpleDateFormat("yyyy年MM月dd日 HH:mm")));
                mDataBinding.tvTemplate6Address.setText(sWeddingBean.getWeddingAddress());
                break;
            case 6:
                mDataBinding.rlTemplate7.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(sWeddingBean.getCoverPath()).into(mDataBinding.ivTemplate7Img);
                mDataBinding.tvTemplate7Groom.setText(sWeddingBean.getGroomName());
                mDataBinding.tvTemplate7Bride.setText(sWeddingBean.getBrideName());
                mDataBinding.tvTemplate7GongLi.setText(TimeUtils.millis2String(time, new SimpleDateFormat("yyyy年MM月dd日 HH:mm")));
                mDataBinding.tvTemplate7Address.setText(sWeddingBean.getWeddingAddress());
                break;
            case 7:
                mDataBinding.rlTemplate8.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(sWeddingBean.getCoverPath()).into(mDataBinding.ivTemplate8Img);
                mDataBinding.tvTemplate8Groom.setText(sWeddingBean.getGroomName());
                mDataBinding.tvTemplate8Bride.setText(sWeddingBean.getBrideName());
                mDataBinding.tvTemplate8GongLi.setText(getString(R.string.template_text7) + TimeUtils.millis2String(time, new SimpleDateFormat("yyyy年MM月dd日 HH:mm")));
                mDataBinding.tvTemplate8NongLi.setText(getString(R.string.template_text8, lunar.getMonthInChinese(), lunar.getDayInChinese()) +
                        " " + TimeUtils.millis2String(time, new SimpleDateFormat("EEE")));
                mDataBinding.tvTemplate8Address.setText(sWeddingBean.getWeddingAddress());
                break;
            case 8:
                mDataBinding.rlTemplate9.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(sWeddingBean.getCoverPath()).into(mDataBinding.ivTemplate9Img);
                mDataBinding.tvTemplate9Groom.setText(sWeddingBean.getGroomName());
                mDataBinding.tvTemplate9Bride.setText(sWeddingBean.getBrideName());
                mDataBinding.tvTemplate9GongLi.setText(getString(R.string.template_text7) + TimeUtils.millis2String(time, new SimpleDateFormat("yyyy年MM月dd日 HH:mm")));
                mDataBinding.tvTemplate9NongLi.setText(getString(R.string.template_text8, lunar.getMonthInChinese(), lunar.getDayInChinese()) +
                        " " + TimeUtils.millis2String(time, new SimpleDateFormat("EEE")));
                mDataBinding.tvTemplate9Address.setText(sWeddingBean.getWeddingAddress());
                break;
        }
    }

    //管理控件
    private void clearView() {
        mDataBinding.rlTemplate1.setVisibility(View.GONE);
        mDataBinding.rlTemplate2.setVisibility(View.GONE);
        mDataBinding.rlTemplate3.setVisibility(View.GONE);
        mDataBinding.rlTemplate4.setVisibility(View.GONE);
        mDataBinding.rlTemplate5.setVisibility(View.GONE);
        mDataBinding.rlTemplate6.setVisibility(View.GONE);
        mDataBinding.rlTemplate7.setVisibility(View.GONE);
        mDataBinding.rlTemplate8.setVisibility(View.GONE);
        mDataBinding.rlTemplate9.setVisibility(View.GONE);
    }

}